// TypeScript file
module Fighter {

    /**
     * 
     */
    export class Ai extends egret.DisplayObjectContainer {

        constructor() {
            super();
            this.AIdifficulty();
        }
        public static AiBool: boolean = false;
        /*AI防守数组**/
        public AiDrefense: any[] = [];
        /**ai发送*/
        public AiFiget: any[] = [];
        /**确定AI难度 */
        public AIdifficulty() {
            // if (!this.AiBool) {
            //     this.AiBool = true;
            //     GameUtil.PlayerMessage.instance.AIdifficulty = Math.random() * 10;
            // }
        }
        public OneTurn() {
            this.AiDrefense = [];
            if (GameUtil.PlayerMessage.instance.game_turns == 1) {
                for (let i = 0; i < GameUtil.PlayerMessage.instance.PlaneNum; i++) {
                    if (Math.floor(Math.random() * 10) >= 5) {
                        GameUtil.PlayerMessage.instance.Aidefense.push(0);
                    }
                }
            }
        }
        public AIPlane() {
            if (!Ai.AiBool) {
                Ai.AiBool = true;
                GameUtil.PlayerMessage.instance.AIdifficulty = Math.random() * 10;
            }
            this.AiDrefense = [];

            // for (let i = 0; i < GameUtil.PlayerMessage.instance.sendEnemy.length; i++) {
            //     this.AiDrefense.push(GameUtil.PlayerMessage.instance.sendEnemy[i]);
            // }

            for (let i = 0; i < GameUtil.PlayerMessage.instance.PlaneNum; i++) {
                GameUtil.PlayerMessage.instance.AiPlane.push(0);
            }
            //简单Ai
            if (GameUtil.PlayerMessage.instance.AIdifficulty > 0 && GameUtil.PlayerMessage.instance.AIdifficulty < 5) {
                for (let i = 0; i < GameUtil.PlayerMessage.instance.game_turns; i++) {
                    var AInum = Math.floor(Math.random() * 10);
                    AInum = AInum % 3
                    GameUtil.PlayerMessage.instance.AiPlane.push(AInum);
                }

                for (let i = 0; i < GameUtil.PlayerMessage.instance.sendEnemy.length; i++) {
                    //console.log(GameUtil.PlayerMessage.instance.sendEnemy.length);
                    var randomNum = Math.random() * 10;
                    randomNum = Math.floor(randomNum);
                    if (randomNum <= 2) {
                        GameUtil.PlayerMessage.instance.sendEnemy.splice(i, 1);
                    }
                }
                for (let i = 0; i < GameUtil.PlayerMessage.instance.sendEnemy.length; i++) {
                    //console.log("普通飞机" + GameUtil.PlayerMessage.instance.sendEnemy[i]);
                    GameUtil.PlayerMessage.instance.Aidefense.push(GameUtil.PlayerMessage.instance.sendEnemy[i]);
                }

            }
            //一般AI
            if (GameUtil.PlayerMessage.instance.AIdifficulty >= 5 && GameUtil.PlayerMessage.instance.AIdifficulty < 8) {
                for (let i = 0; i < GameUtil.PlayerMessage.instance.game_turns * 2; i++) {
                    var AInum = Math.random() * 10;
                    AInum = Math.floor(AInum);
                    AInum = AInum % 3

                    GameUtil.PlayerMessage.instance.AiPlane.push(AInum);

                }

                for (let i = 0; i < GameUtil.PlayerMessage.instance.sendEnemy.length; i++) {

                    var randomNum = Math.random() * 10;
                    randomNum = Math.floor(randomNum);
                    if (randomNum <= 5) {
                        GameUtil.PlayerMessage.instance.sendEnemy.splice(i, 1);

                    }
                }

                for (let i = 0; i < GameUtil.PlayerMessage.instance.sendEnemy.length; i++) {
                    GameUtil.PlayerMessage.instance.Aidefense.push(GameUtil.PlayerMessage.instance.sendEnemy[i]);
                }
                GameUtil.PlayerMessage.instance.sendEnemy = [];
            }
            //困难AI
            if (GameUtil.PlayerMessage.instance.AIdifficulty >= 8 && GameUtil.PlayerMessage.instance.AIdifficulty <= 10) {
                for (let i = 0; i < GameUtil.PlayerMessage.instance.game_turns * 3; i++) {
                    var AInum = Math.random() * 10;
                    AInum = Math.floor(AInum);
                    AInum = AInum % 3
                    GameUtil.PlayerMessage.instance.AiPlane.push(AInum);
                }

                for (let i = 0; i < GameUtil.PlayerMessage.instance.sendEnemy.length; i++) {
                    //console.log(GameUtil.PlayerMessage.instance.sendEnemy.length);
                    var randomNum = Math.random() * 10;
                    randomNum = Math.floor(randomNum);
                    if (randomNum <= 7) {
                        GameUtil.PlayerMessage.instance.sendEnemy.splice(i, 1);

                    }
                }

                for (let i = 0; i < GameUtil.PlayerMessage.instance.sendEnemy.length; i++) {
                    GameUtil.PlayerMessage.instance.Aidefense.push(GameUtil.PlayerMessage.instance.sendEnemy[i]);
                }
                GameUtil.PlayerMessage.instance.sendEnemy = [];
            }
        }
    }

}